package com.bei_en.teacher.admin.controller.questions;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.questions.bean.QuestionsType;
import com.bei_en.teacher.admin.model.questions.bean.QuestionsType2;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsArticleCondition;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsArticlePraiseCondition;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsTypeCondition;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsTypeQueryCondition;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsArticlePraiseVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsArticleVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsTypeVo;
import com.bei_en.teacher.admin.model.regions.condition.RegionsCondition;
import com.bei_en.teacher.admin.model.users.vo.UsersVo;
import com.bei_en.teacher.admin.other.Consts;
import com.bei_en.teacher.admin.service.article.ArticleTypeService;
import com.bei_en.teacher.admin.service.questions.QuestionsArticlePraiseService;
import com.bei_en.teacher.admin.service.questions.QuestionsArticleService;
import com.bei_en.teacher.admin.service.questions.QuestionsTypeService;
import com.bei_en.teacher.admin.service.regions.RegionsService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.util.interceptor.OAuthRequired;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.bei_en.common.web.MySessionContext;
import com.mysql.fabric.xmlrpc.base.Array;

/**
 * 
 * @author Guocg
 * @date 2016-8-18 9:18:37
 */
@Controller
@RequestMapping("/api/Questions")
public class QuestionsTypeAPPController extends BaseController {

	@Autowired
	QuestionsTypeService questionsTypeService;
	@Autowired
	QuestionsArticleService questionsArticleService;
	@Autowired
	QuestionsArticlePraiseService questionsArticlePraiseService;
    @Autowired
    RegionsService regionsService;
    @RequestMapping("/questionsTypeList")
   	@ResponseBody
       public JsonResult questionsTypeList(Model model,@Valid  QuestionsTypeCondition vo, BindingResult br, HttpSession session ) {
       	if (br.hasErrors())
          		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
   					.getDefaultMessage());
        	try {
        	   List<QuestionsTypeVo> list =new ArrayList<QuestionsTypeVo>();
        	   vo.setOrderDirection("asc");
        	   if(vo.getType()==null){
        		   vo.setType(1);
        	   }
   		   list = questionsTypeService.findQuestionsTypePage(vo);
   		   JSONArray array=new JSONArray();
   		   JSONObject object=null;
   		   QuestionsTypeVo questionsTypeVo=null;
   		   for (int i = 0; i < list.size(); i++) {
   			   object=new JSONObject();
   			   questionsTypeVo=list.get(i);
   	   seetingJsonObject(object,"Id",  questionsTypeVo.getId());
   	   seetingJsonObject(object,"name",  questionsTypeVo.getName());
   	   seetingJsonObject(object,"parent",  questionsTypeVo.getParent());
   	   
   	   seetingJsonObject(object,"type",  questionsTypeVo.getType());
   	   seetingJsonObject(object,"year",  questionsTypeVo.getYear());
   	   seetingJsonObject(object,"provinceId",  questionsTypeVo.getProvinceId());
   	   seetingJsonObject(object,"cityId",  questionsTypeVo.getCityId());
   	   seetingJsonObject(object,"areaId",  questionsTypeVo.getAreaId());
   	   seetingJsonObject(object,"thumbnail",  questionsTypeVo.getThumbnail());
   	   seetingJsonObject(object,"educationId",  questionsTypeVo.getEducationId());
   	   seetingJsonObject(object,"subjectId",  questionsTypeVo.getSubjectId());
   	   seetingJsonObject(object,"questionsSum",  questionsTypeVo.getQuestionsSum());
   	   seetingJsonObject(object,"studyNum",  questionsTypeVo.getStudyNum());
   	   seetingJsonObject(object,"makeFraction",  questionsTypeVo.getMakeFraction());
   	   seetingJsonObject(object,"makeTime",  questionsTypeVo.getMakeTime());
  	   seetingJsonObject(object,"makeNum",  questionsTypeVo.getMakeNum());
  	   seetingJsonObject(object,"yesNum",  questionsTypeVo.getYesNum());
   	   seetingJsonObject(object,"weight",  questionsTypeVo.getWeight());
   	   seetingJsonObject(object,"fractionSum",  questionsTypeVo.getFractionSum());
   	   seetingJsonObject(object,"createTime",  questionsTypeVo.getCreateTime().getTime());
   			   array.add(object);
   		}
   		return new JsonSuccessResult(session.getId(), array);
   		
        	} catch (Exception e) {
   			e.printStackTrace();
   			return new JsonFailResult(session.getId(), 1, "后台报错",
   					e.getMessage());
   		}
       }
	/**
	 * 章节点详情
	 * @param model
	 * @param vo
	 * @param br
	 * @param session
	 * @return
	 */
	@RequestMapping("/questionsTypeChapterPointDetails")
	@ResponseBody
	public JsonResult questionsTypeChapterPointDetails(Model model,
			@Valid QuestionsTypeCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		try {
			JSONObject object = null;
			QuestionsTypeVo questionsTypeVo = null;
		
				object = new JSONObject();
				questionsTypeVo =questionsTypeService.findById(vo.getId());
				  object.put("Id",  questionsTypeVo.getId());
				object.put("name", questionsTypeVo.getName());
				   object.put("name",  questionsTypeVo.getName());
				   object.put("thumbnail",  questionsTypeVo.getThumbnail());
				   object.put("studyNum",  questionsTypeVo.getStudyNum());
				   object.put("makeFraction",  questionsTypeVo.getMakeFraction());
				   object.put("makeTime",  questionsTypeVo.getMakeTime());
				// 根据节点id得到对应的分类和内容
					QuestionsArticleCondition questionsArticleCondition = new QuestionsArticleCondition();
					questionsArticleCondition.setQuestionsType(vo.getId());
					List<QuestionsArticleVo> questionsArticleVos = questionsArticleService
							.findQuestionsArticlePage2(questionsArticleCondition);
					QuestionsArticleVo questionsArticleVo;
					JSONArray array = new JSONArray();
					JSONObject object2;
					for (int i = 0; i < questionsArticleVos.size(); i++) {
						questionsArticleVo = questionsArticleVos.get(i);
						object2 = new JSONObject();
						object2.put(
								"content",
								""
										+ "<p align=\"center\"><span style=\"font-size:18px;\"><strong>"
										+ questionsArticleVo.getTitle()
										+ "</strong></span></p><p><span style=\"color:#CCCCCC;\">时间:  "
										+ "  </span></p><p></p>"
										+ questionsArticleVo.getContent());
						object2.put("title", questionsArticleVo.getArticleTypes()
								.getName());
						object2.put("articleId", questionsArticleVo.getId());
						UsersVo user = (UsersVo) session
								.getAttribute(Consts.LOGIN_USER);
						if (user == null) {
							object2.put("praise", "no");
						} else {

							QuestionsArticlePraiseCondition questionsArticlePraiseCondition = new QuestionsArticlePraiseCondition();
							questionsArticlePraiseCondition.setPraisePeople(user
									.getUserId());
							List<QuestionsArticlePraiseVo> lists = questionsArticlePraiseService
									.findQuestionsArticlePraisePage(questionsArticlePraiseCondition);
							if (!lists.isEmpty()) {
								object2.put("praise", "yes");
							} else {
								object2.put("praise", "no");
							}
						}

						array.add(object2);
					}
//			
//					object2 = new JSONObject();
//					object2.put("content", "");
//					switch(vo.getSortMode()){
//					case 0:
//						object2.put("title", "本章小节");
//						break;
//					case 1:
//						object2.put("title", "本节要点");
//						break;
//					case 2:
//						object2.put("title", "知识巩固");
//						break;
//						
//					}
//			
//					object2.put("articleId", "");
//					object2.put("praise", "no");
//					array.add(object2);
					object.put("list", array); 

			return new JsonSuccessResult(session.getId(), object);

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}
	/**
	 * 章节点列表
	 * @param model
	 * @param vo
	 * @param br
	 * @param session
	 * @return
	 */
	@RequestMapping("/questionsTypeChapterPointList")
	@ResponseBody
	public JsonResult questionsTypeChapterPoint(Model model,
			@Valid QuestionsTypeQueryCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		try {
			if(vo.getType()==null){
				vo.setType(1);
			}
			List<QuestionsTypeVo> list = new ArrayList<QuestionsTypeVo>();
			list = questionsTypeService.findQuestionsTypePage(vo);
			JSONArray array = new JSONArray();
			JSONObject object = null;
			QuestionsTypeVo questionsTypeVo = null;
			for (int i = 0; i < list.size(); i++) {
				object = new JSONObject();
				questionsTypeVo = list.get(i);
				  object.put("Id",  questionsTypeVo.getId());
				object.put("name", questionsTypeVo.getName());
				if(vo.getType()==2){
//				if(	questionsTypeVo.getProvinceId()==0){
//					  object.put("provinceId", "全国");
//					  
//					  
//				}else{
//					  object.put("provinceId",regionsService.findById(questionsTypeVo.getProvinceId()).getItemName());
//					  object.put("time","待定");
//				}
				 	   seetingJsonObject(object,"questionsSum",  questionsTypeVo.getQuestionsSum());
				   	   seetingJsonObject(object,"studyNum",  questionsTypeVo.getStudyNum());
				   	   seetingJsonObject(object,"makeFraction",  questionsTypeVo.getMakeFraction());
				   	   seetingJsonObject(object,"makeTime",  questionsTypeVo.getMakeTime());
				  	   seetingJsonObject(object,"makeNum",  questionsTypeVo.getMakeNum());
				  	   seetingJsonObject(object,"yesNum",  questionsTypeVo.getYesNum());
				   	   seetingJsonObject(object,"weight",  questionsTypeVo.getWeight());
				   	   seetingJsonObject(object,"fractionSum",  questionsTypeVo.getFractionSum());
				}
				array.add(object);
			}
			return new JsonSuccessResult(session.getId(), array);

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	/**
	 * 知识点列表
	 * 
	 * @param model
	 * @param vo
	 * @param br
	 * @param session
	 * @return
	 */
	@RequestMapping("/questionsType")
	@ResponseBody
	public JsonResult questionsType(Model model,
			@Valid QuestionsTypeCondition vo, BindingResult br,
			HttpSession session) {

		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		try {

			List<QuestionsType2> list = new ArrayList<QuestionsType2>();
			int sort = 0;
			if (vo.getSortMode() != null) {
				sort = vo.getSortMode();
			}

			switch (sort) {
			case 1:
				vo.setOrderField("spot_id");
				break;
			case 2:
				vo.setOrderField("spot_weight");
				break;
			case 3:
				vo.setOrderField("spot_questions_sum");
				break;
			case 4:
				vo.setOrderField("spot_study_num");
				break;
			default:
				break;
			}
			list = questionsTypeService.findQuestionsTypePage2(vo);
			JSONArray array = new JSONArray();
			JSONObject object;
			QuestionsType2 oQuestionsType2;
			for (int i = 0; i < list.size(); i++) {
				oQuestionsType2 = list.get(i);
				object = new JSONObject();
				object.put("Id", oQuestionsType2.getSpotId());
				object.put("name", oQuestionsType2.getSpotName());
				object.put("thumbnail", oQuestionsType2.getSpotThumbnail());
				object.put("questionsSum",
						oQuestionsType2.getSpotQuestionsSum());
				object.put("studyNum", oQuestionsType2.getSpotStudyNum());
				object.put("weight", oQuestionsType2.getSpotWeight());
				array.add(object);
			}
			return new JsonSuccessResult(session.getId(), array);

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	/**
	 * 知识点详情
	 * 
	 * @param model
	 * @param vo
	 * @param br
	 * @param session
	 * @return
	 */
	@RequestMapping("/questionsTypeDetails")
	@ResponseBody
	public JsonResult questionsTypeDetails(Model model,
			@Valid QuestionsTypeCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		try {
			if (vo.getId() == null) {
				return new JsonFailResult(session.getId(), 1, "id不能为空");
			}
			// 根据节点id得到对应的分类和内容
			QuestionsArticleCondition questionsArticleCondition = new QuestionsArticleCondition();
			questionsArticleCondition.setQuestionsType(vo.getId());
			List<QuestionsArticleVo> questionsArticleVos = questionsArticleService
					.findQuestionsArticlePage2(questionsArticleCondition);
			QuestionsArticleVo questionsArticleVo;
			JSONArray array = new JSONArray();
			JSONObject object;
			for (int i = 0; i < questionsArticleVos.size(); i++) {
				questionsArticleVo = questionsArticleVos.get(i);
				object = new JSONObject();
				object.put(
						"content",
						""
								+ "<p align=\"center\"><span style=\"font-size:18px;\"><strong>"
								+ questionsArticleVo.getTitle()
								+ "</strong></span></p><p><span style=\"color:#CCCCCC;\">时间:  "
								+ "  </span></p><p></p>"
								+ questionsArticleVo.getContent());
				object.put("title", questionsArticleVo.getArticleTypes()
						.getName());
				object.put("articleId", questionsArticleVo.getId());
				UsersVo user = (UsersVo) session
						.getAttribute(Consts.LOGIN_USER);
				if (user == null) {
					object.put("praise", "no");
				} else {

					QuestionsArticlePraiseCondition questionsArticlePraiseCondition = new QuestionsArticlePraiseCondition();
					questionsArticlePraiseCondition.setPraisePeople(user
							.getUserId());
					List<QuestionsArticlePraiseVo> lists = questionsArticlePraiseService
							.findQuestionsArticlePraisePage(questionsArticlePraiseCondition);
					if (!lists.isEmpty()) {
						object.put("praise", "yes");
					} else {
						object.put("praise", "no");
					}
				}

				array.add(object);

			}
			object = new JSONObject();
			object.put("content", "");
			object.put("title", "知识巩固");
			object.put("articleId", "");
			object.put("praise", "no");
			array.add(object);

			return new JsonSuccessResult(session.getId(), array);

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/questionsTypeAdd")
	@ResponseBody
	public JsonResult questionsTypeAdd(Model model,
			@Valid QuestionsTypeCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		try {

			QuestionsTypeVo questionsType = new QuestionsTypeVo();
			questionsTypeService.insert(questionsType);
			return new JsonSuccessResult(session.getId(), "成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/questionsTypeEdit")
	public JsonResult questionsTypeEdit(Model model,
			@Valid QuestionsTypeCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
			QuestionsTypeVo questionsType = new QuestionsTypeVo();

			questionsTypeService.updateSelective(questionsType);

			return new JsonSuccessResult(session.getId(), "成功");

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

}
